package com.shaoshaohuo.app.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.utils.ToastUtil;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 司机订单详情页
 * Created by Administrator on 2017/6/26.
 */

public class CaeDeltaActivirt extends BaseActivity {

    private ListView listView;
    private String orderid;
    private int pos;
    private Myadapter myadapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_cardelat_layout
        );
        Intent intent = getIntent();
        orderid = intent.getStringExtra("orderid");
        pos = intent.getIntExtra("pos", -1)+1;
        Log.e("TAG", "onCreate: "+pos );
        listView = (ListView) findViewById(R.id.car_date_list);
        myadapter = new Myadapter();
        listView.setAdapter(myadapter);
        findViewById(R.id.iv_bacj).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        network();
    }

    private void network() {

        ShopHttpConfig.cardletadriverqurey(this, orderid, pos + "", new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {

            }

            @Override
            public void suseff(Call call, Response response, Object o) {
                final CarDletaEntity carDletaEntity = (CarDletaEntity) o;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        myadapter.setdate(carDletaEntity.getData());
                    }
                });
            }
        }, CarDletaEntity.class);

    }

    class Myadapter extends BaseAdapter implements View.OnClickListener {
        private List<CarDletaEntity.DataBean> list = new ArrayList<>();

        public void setdate(List<CarDletaEntity.DataBean> list) {
            this.list = list;
            notifyDataSetChanged();
        }

        public List<CarDletaEntity.DataBean> getdate() {
            return list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Myholder myholder;
            if (convertView == null) {
                myholder = new Myholder();
                convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.caedeltalistitem, null);
                myholder.tv_orderid = (TextView) convertView.findViewById(R.id.orderid);
                myholder.tv_reName = (TextView) convertView.findViewById(R.id.reName);
                myholder.tv_rePhone = (TextView) convertView.findViewById(R.id.rePhone);
                myholder.img = (ImageView) convertView.findViewById(R.id.img);
                myholder.btn_sure = (TextView) convertView.findViewById(R.id.btn_sure);
                convertView.setTag(myholder);
            } else {
                myholder = (Myholder) convertView.getTag();
            }
            CarDletaEntity.DataBean dataBean = list.get(position);
            String buy_num = dataBean.getBuy_num();
            String img = dataBean.getImg();
            String sonid = dataBean.getSonid();
            String proName = dataBean.getProName();
            myholder.tv_rePhone.setText("数量：" + buy_num);
            myholder.tv_reName.setText("商品名称：" + proName);
            myholder.tv_orderid.setText("订单号：" + sonid);
            myholder.btn_sure.setTag(position);
            myholder.btn_sure.setOnClickListener(this);
            if (pos == 1) {
                myholder.btn_sure.setVisibility(View.VISIBLE);
            } else {
                myholder.btn_sure.setVisibility(View.GONE);
            }
            if(img.length()>0){
                Picasso.with(getApplicationContext()).load(img).into(myholder.img);
            }else {
                myholder.img.setImageResource(R.mipmap.logo);
            }

            return convertView;
        }

        @Override
        public void onClick(View v) {
            final int pos = (int) v.getTag();
            CarDletaEntity.DataBean dataBean = list.get(pos);
            String fatherid = dataBean.getFatherid();
            ShopHttpConfig.ConfirmReceipt(getApplicationContext(), "2", fatherid, new Okhttputils.OkutListener() {
                @Override
                public void error(Call call, IOException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtil.toast("失败");
                        }
                    });
                }

                @Override
                public void error(Call call, int code) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtil.toast("失败");
                        }
                    });
                }

                @Override
                public void suseff(Call call, Response response, Object o) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtil.toast("成功");
                            list.remove(pos);
                            notifyDataSetChanged();
                        }
                    });
                }
            }, BaseEntity.class);

        }

        class Myholder {
            TextView tv_orderid,
                    tv_reName,
                    tv_rePhone;
            ImageView img;
            TextView btn_sure;
        }
    }
}
